package CtrLayer;
import java.util.Date;
import java.util.Iterator;
import ModelLayer.ProductList;
import ModelLayer.Product;


public class ProductCtr
{
    private static ProductList instance = ProductList.getInstance();

    public ProductCtr()
    {
     
    }

    public Product getProduct(int id) 
    {
        return instance.getProduct(id);
    }
    
    public void addProduct(int idP, String name, String desc, double price, String location)
    {
        Product newProduct = new Product(idP,name,desc,price,location);
        instance.addProduct(newProduct);
    }
    
    public boolean removeProduct(int idP)
    {
        return instance.removeProduct(idP);
    }
    
    public boolean updateProduct(int idP,String updateValue,int opt)
    {
        Product prod = getProduct(idP);
        try
        {
            switch(opt)
            {
                case 1 : { prod.setName(updateValue);  }; break;
                case 2 : { prod.setDescription(updateValue);  }; break;
                case 3 : { prod.setStock(Integer.parseInt(updateValue));  }; break;
                case 4 : { prod.setPrice(Double.parseDouble(updateValue));  }; break;
                case 5 : { prod.setLocation(updateValue);  }; break;
                case 6 : { prod.addPiece(getProduct(Integer.parseInt(updateValue))); }; break;
                case 7 : { prod.removePiece(Integer.parseInt(updateValue)); }; break;
            }
            return true;
        }
        catch(Exception e) {}
        return false;
    }
    
    public Iterator getProductsIterator()
    {
        return instance.getProductsIterator();
    }
    
    public boolean setDiscount(int idP, double disc, Date endDate)
    {
        Product prod = getProduct(idP);
        try{
            prod.setDiscount(disc, endDate);
            return true;
        }
        catch(Exception e){ }
        
        return false;
    }
}
